#include<iostream>
#include<algorithm>
#include<cstring>
#include<string>
#include<vector>
#include<queue>
#include<deque>
#include<map>
#include<set>
#include<unordered_map>
#define inf 0x3f3f3f3f
using namespace std;
using LL = long long;
using ULL = unsigned long long;
typedef pair<int, int> PII;
const int N = 1e5 + 10;
int n, m;
int fa[N], d[N];
int res;

int find(int x){
    if(fa[x]!=x){
        int t = find(fa[x]);
        d[x] += d[fa[x]];
        fa[x] = t;
    }
    return fa[x];
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cin >> n >> m;
    for (int i = 1; i <= n;i++)
        fa[i] = i;
    while(m--){
        int u, x, y;
        cin >> u >> x >> y;
        if(x>n || y>n){
            res++;
            continue;
        }
        int px = find(x), py = find(y);
        if(u==1){
            if(px!=py){
                fa[px] = py;
                d[px] = d[y] - d[x];
            }
            else{
                if((d[x]-d[y])%3)
                    res++;
            }
        }
        else{
            if(px!=py){
                fa[px] = py;
                d[px] = d[y] - d[x] + 1;
            }
            else{
                if((d[x]-d[y]-1)%3)
                    res++;
            }
        }
    }
    cout << res;
    return 0;
}